home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / MysticView / source / LoadDTPicture.c < prev    next >
C/C++ Source or Header  |  1996-12-26  |  7KB  |  240 lines

  1.  
  2. /*********************************************************************
  3. ----------------------------------------------------------------------
  4.  
  5.     $VER: LoadDTPicture 10.0
  6.     ©1996 TEK neoscientists
  7.  
  8.     based upon ViewDT.c © by Cloanto Software.
  9.     Slightly modified.
  10.  
  11. ----------------------------------------------------------------------
  12. *********************************************************************/
  13.  
  14. #include <exec/memory.h>
  15. #include <graphics/gfx.h>
  16. #include <graphics/displayinfo.h>
  17. #include <intuition/gadgetclass.h>
  18. #include <datatypes/datatypes.h>
  19. #include <datatypes/datatypesclass.h>
  20. #include <datatypes/pictureclass.h>
  21. #include <proto/exec.h>
  22. #include <proto/dos.h>
  23. #include <proto/graphics.h>
  24. #include <proto/intuition.h>
  25. #include <proto/datatypes.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28.  
  29. #include "LoadDTPicture.h"
  30.  
  31. /*********************************************************************
  32. ----------------------------------------------------------------------
  33.  
  34.     success = IsDataTypes ( filename,namebuf,nbufsize )    
  35.  
  36.       file_name: name of the file to inspect
  37.       name_buff: (optional) buffer to store the file format name
  38.       nbuff_size: size of name_buff
  39.  
  40.    Return value
  41.       TRUE if DataTypes recognized the file as a valid picture file
  42.       FALSE otherwise
  43.  
  44. ----------------------------------------------------------------------
  45. *********************************************************************/
  46.  
  47. BOOL IsDataTypes(UBYTE *file_name, UBYTE *name_buff, LONG nbuff_size)
  48. {
  49.    struct DataType *dtn;
  50.    struct DataTypeHeader *dth;
  51.    BPTR lock;
  52.    BOOL it_is;
  53.  
  54.    it_is = FALSE;
  55.    if (lock = Lock(file_name, ACCESS_READ))
  56.    {
  57.       /* inspect file */
  58.       if (dtn = ObtainDataTypeA(DTST_FILE, (APTR)lock, NULL))
  59.       {
  60.          dth = dtn->dtn_Header;
  61.          if (dth->dth_GroupID == GID_PICTURE)   /* is it a picture? */
  62.          {
  63.             it_is = TRUE;
  64.             if (name_buff)
  65.             {
  66.                strncpy(name_buff, dth->dth_Name, nbuff_size);
  67.                *(name_buff + nbuff_size - 1) = 0;  /* safe strncpy() termination */
  68.             }
  69.          }
  70.          ReleaseDataType(dtn);
  71.       }
  72.       UnLock(lock);
  73.    }
  74.    return(it_is);
  75. }
  76.  
  77.  
  78. /*********************************************************************
  79. ----------------------------------------------------------------------
  80.  
  81.     error = GetDataTypesPicture ( filename,pic,bmap_flags )    
  82.  
  83.    Parameters
  84.       file_name: name of the file to load
  85.       pic: work structure
  86.       bmap_flags: AllocBitMap() flags (BMF_DISPLAYABLE, BMF_INTERLEAVED etc.)
  87.  
  88.    Return value
  89.       0 if successful (picture info and data in "pic" structure) or
  90.       error code as from dos.library IoErr() function
  91.  
  92. ----------------------------------------------------------------------
  93. *********************************************************************/
  94.  
  95. LONG GetDataTypesPicture(UBYTE *file_name, struct Picture *pic, ULONG bmap_flags)
  96. {
  97.    Object *obj;
  98.    struct BitMapHeader *bmh;
  99.    struct BitMap *bmap;
  100.    struct gpLayout layout;
  101.    ULONG *creg, *ctab;
  102.    UBYTE *str;
  103.    LONG ncol, crsize, err;
  104.  
  105.    memset(pic, 0, sizeof(struct Picture));   /* clear pic structure */
  106.    err = 0;
  107.  
  108.    if (obj = NewDTObject(file_name,
  109.                          DTA_SourceType, DTST_FILE,
  110.                          DTA_GroupID, GID_PICTURE,
  111.                          PDTA_Remap, FALSE,
  112.                          TAG_DONE))    /* get the picture object */
  113.    {
  114.       if (GetDTAttrs(obj,
  115.                      PDTA_ModeID, &pic->display_ID,
  116.                      PDTA_BitMapHeader, &bmh,
  117.                      TAG_DONE) == 2)   /* get the bitmap_header and mode_id */
  118.       {
  119.          pic->bmhd = *bmh;
  120.  
  121.          /*
  122.             query the object about its author, copyright and annotation
  123.          */
  124.          if (GetDTAttrs(obj, DTA_ObjAuthor, &str, TAG_DONE) == 1)
  125.          {
  126.             if (str)
  127.             {
  128.                pic->author_size = strlen(str) + 1;
  129.                if (pic->author = AllocMem(pic->author_size, 0))
  130.                   strcpy(pic->author, str);
  131.             }
  132.          }
  133.          if (GetDTAttrs(obj, DTA_ObjCopyright, &str, TAG_DONE) == 1)
  134.          {
  135.             if (str)
  136.             {
  137.                pic->copyright_size = strlen(str) + 1;
  138.                if (pic->copyright = AllocMem(pic->copyright_size, 0))
  139.                   strcpy(pic->copyright, str);
  140.             }
  141.          }
  142.          if (GetDTAttrs(obj, DTA_ObjAnnotation, &str, TAG_DONE) == 1)
  143.          {
  144.             if (str)
  145.             {
  146.                pic->annotation_size = strlen(str) + 1;
  147.                if (pic->annotation = AllocMem(pic->annotation_size, 0))
  148.                   strcpy(pic->annotation, str);
  149.             }
  150.          }
  151.  
  152.          layout.MethodID = DTM_PROCLAYOUT;   /* render the object */
  153.          layout.gpl_GInfo = NULL;
  154.          layout.gpl_Initial = TRUE;
  155.  
  156.          if (DoDTMethodA(obj, NULL, NULL, (Msg)&layout))
  157.          {
  158.             if (GetDTAttrs(obj,
  159.                            PDTA_BitMap, &bmap,
  160.                            PDTA_CRegs, &creg,
  161.                            PDTA_NumColors, &ncol,
  162.                            TAG_DONE) == 3)   /* get the bitmap and its colors */
  163.             {
  164.                if (bmap != NULL && creg != NULL && ncol != 0)
  165.                {
  166.                   crsize = (ncol * 3) * 4;
  167.                   pic->palette_entries = ncol;
  168.                   pic->palette_size = crsize + (2 * 4);  /* LoadRGB32() table requirements */
  169.  
  170.                   if (pic->palette = AllocMem(pic->palette_size, 0))
  171.                   {
  172.                      ctab = pic->palette;
  173.                      *ctab++ = (ncol << 16) | 0;   /* number of colors and first color to load */
  174.                      memcpy(ctab, creg, crsize);
  175.                      *(ctab + (crsize / 4)) = 0;   /* terminator */
  176.                   }
  177.                   else err = ERROR_NO_FREE_STORE;
  178.  
  179.                   if (pic->bmap = AllocBitMap(pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, pic->bmhd.bmh_Depth, bmap_flags, NULL))
  180.                   {
  181.                      BltBitMap(bmap, 0,0, pic->bmap, 0,0, pic->bmhd.bmh_Width, pic->bmhd.bmh_Height, 0xC0, 0xFF, NULL);
  182.                      WaitBlit();
  183.                   }
  184.                   else err = ERROR_NO_FREE_STORE;
  185.                }
  186.                else err = ERROR_REQUIRED_ARG_MISSING;
  187.             }
  188.             else err = IoErr();
  189.          }
  190.          else err = IoErr();
  191.       }
  192.       else err = ERROR_REQUIRED_ARG_MISSING;
  193.  
  194.       DisposeDTObject(obj);   /* free the object */
  195.    }
  196.    else err = IoErr();
  197.  
  198.    if (err)
  199.       FreePicture(pic);
  200.  
  201.    return(err);
  202. }
  203.  
  204.  
  205. /*********************************************************************
  206. ----------------------------------------------------------------------
  207.  
  208.     FreePicture ( pic )    
  209.  
  210.    Parameters
  211.       pic: Picture structure with resources to free
  212.  
  213.    Return value
  214.       none
  215.  
  216. ----------------------------------------------------------------------
  217. *********************************************************************/
  218.  
  219. void FreePicture(struct Picture *pic)
  220. {
  221.    if (pic->bmap)
  222.    {
  223.       WaitBlit();
  224.       FreeBitMap(pic->bmap);
  225.    }
  226.    if (pic->palette)
  227.       FreeMem(pic->palette, pic->palette_size);
  228.  
  229.    if (pic->author)
  230.       FreeMem(pic->author, pic->author_size);
  231.  
  232.    if (pic->copyright)
  233.       FreeMem(pic->copyright, pic->copyright_size);
  234.  
  235.    if (pic->annotation)
  236.       FreeMem(pic->annotation, pic->annotation_size);
  237.  
  238.    memset(pic, 0, sizeof(struct Picture));   /* clear it all */
  239. }
  240.